//
//  GoodCreateVC.m
//  danpin
//
//  Created by chuangjia on 21/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "GoodCreateVC.h"
#import "CategarySelectVC.h"
#import "BrandSelectVC.h"
#import "GoodSelectVC.h"
#import "CellConfig.h"
#import "ShaiDanVC.h"
#import "ZToastManager.h"

@interface GoodCreateVC ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UITextFieldDelegate>
{
    
}
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,strong) UITextField     *txtPrice;
@property(nonatomic,strong) UITextField     *txtTitle;
@property(nonatomic,strong)NSMutableArray *setcionArray;
@property(nonatomic,strong)NSMutableArray *pictureArray;
@property(nonatomic,strong)NSMutableArray *ImageArray;
@property(nonatomic,strong)NSMutableArray *TextArray;
@property(nonatomic,assign)BOOL isCategoryEnable;
@property(nonatomic,strong) NSString *cateId;
@property(nonatomic,strong) NSString *cateString;
@property(nonatomic,strong) NSDictionary *cateDictionary;

@property(nonatomic,strong) NSString *brandId;
@property(nonatomic,strong) NSString *brandString;
@property(nonatomic,strong) NSString *cnameId;
@property(nonatomic,strong) NSString *cnameString;
@property(nonatomic,strong) NSString *priceString;

@end

@implementation GoodCreateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view
    
    self.view.backgroundColor =[UIColor whiteColor];
    self.rightTitle =@"完成";
    self.navTitle =@"新建商品";
    //
    _isCategoryEnable =NO;
    _cateId =@"";
    _cateString =@"";
    _brandId =@"";
    _brandString=@"";
    _cnameId =@"";
    _cnameString=@"";
    _priceString=@"";
    //
    if (!_setcionArray) {
        _setcionArray =[[NSMutableArray alloc] init];
    }
    _setcionArray =[NSMutableArray arrayWithObjects:@"品名:",@"价格:",@"分类:",@"品牌:", nil];
    //
    
    if (!_pictureArray) {
        _pictureArray =[[NSMutableArray alloc] init];
    }
    if (!_ImageArray) {
        _ImageArray =[[NSMutableArray alloc] init];
    }
    [_ImageArray removeAllObjects];
    
    if (!_TextArray) {
        _TextArray =[[NSMutableArray alloc] init];
    }
    [_TextArray removeAllObjects];
    
    //
    [self initUI];
   
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 0:
        {
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 1:
        {
            [self okAction];
        }
            break;
        default:
            break;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
      DLog(@"%s", __PRETTY_FUNCTION__);
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_setcionArray removeAllObjects];
    _setcionArray=nil;
    [_pictureArray removeAllObjects];
    _pictureArray=nil;
    [_ImageArray removeAllObjects];
    _ImageArray=nil;
    [_TextArray removeAllObjects];
    _TextArray=nil;
    DLog(@"%s", __PRETTY_FUNCTION__);
}

//
-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navHeight, self.view.bounds.size.width, self.view.bounds.size.height-self.navHeight) style:UITableViewStyleGrouped];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    _tbView.backgroundColor =[UIColor whiteColor];
    _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbView.showsVerticalScrollIndicator = NO;
    _tbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.tbView.tableFooterView =[ZShare footerView:@""];
    [self.view addSubview:_tbView];
}
#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return self.setcionArray.count;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 44;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return 0.01;
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
    if (indexPath.section==0)
    {
        UILabel *titleLabel =nil;
        UILabel *contentLabel =nil;
        UIView *lineView =nil;
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*2, 0, 50, cell.frame.size.height)];
            [titleLabel setBackgroundColor:[UIColor clearColor]];
            [titleLabel setTextAlignment:NSTextAlignmentCenter];
            [titleLabel setFont:FONT(14)];
            [titleLabel setTextColor:[UIColor darkGrayColor]];
            [cell.contentView addSubview:titleLabel];
            
            contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(50+PHOTO_FRAME_WIDTH*2, 0,DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4-50, cell.frame.size.height)];
            [contentLabel setBackgroundColor:[UIColor clearColor]];
            [contentLabel setTextAlignment:NSTextAlignmentLeft];
            [contentLabel setFont:FONT(13)];
            [contentLabel setTextColor:[UIColor lightGrayColor]];
            [cell.contentView addSubview:contentLabel];
            lineView =[[UIView alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*2, cell.frame.size.height-0.5,DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, 0.5)];
            lineView.backgroundColor =UIColorFromRGB(0xcccccc);
            [cell.contentView addSubview:lineView];
        }
        NSString *title =[self.setcionArray objectAtIndex:indexPath.row];
        switch (indexPath.row) {
            case 2:
            {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                titleLabel.text =title;
                if (_cateString.length>0) {
                    contentLabel.text = [NSString stringWithFormat:@"%@",_cateString];
                }else{
                    contentLabel.text = [NSString stringWithFormat:@"%@",@"可选"];
                }
                
            }
                break;
            case 3:
            {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                if (_isCategoryEnable) {
                    titleLabel.textColor = [UIColor darkGrayColor];
                }
                else{
                    titleLabel.textColor = [UIColor lightGrayColor];
                }
                titleLabel.text =title;
                cell.textLabel.font = FONT(14);
                if (_brandString.length>0) {
                    contentLabel.text = [NSString stringWithFormat:@"%@",_brandString];
                }else{
                    contentLabel.text = [NSString stringWithFormat:@"%@",@"可选"];
                }
            }
                break;
            case 0:
            {
                titleLabel.textColor = [UIColor darkGrayColor];
                titleLabel.text =title;
                self.txtTitle.frame =CGRectMake(50+PHOTO_FRAME_WIDTH*2, 1, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4-50, cell.frame.size.height);
                [cell addSubview:self.txtTitle];
                
            }
                break;
            case 1:
            {
                titleLabel.textColor = [UIColor darkGrayColor];
                titleLabel.text =title;
                self.txtPrice.frame =CGRectMake(50+PHOTO_FRAME_WIDTH*2, 1, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4-50, cell.frame.size.height);
                [cell addSubview:self.txtPrice];
            }
                break;
            default:
                break;
        }
        return cell;
    }
    
    return cell;
}
-(UITextField *)txtPrice
{
    if (!_txtPrice) {
        self.txtPrice = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtPrice.textColor =[UIColor lightGrayColor];
        [self.txtPrice setBorderStyle:UITextBorderStyleNone];
        self.txtPrice.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtPrice.font = FONT(13);
        self.txtPrice.placeholder =@"选填";
        self.txtPrice.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtPrice.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtPrice.returnKeyType = UIReturnKeyDone;
        self.txtPrice.keyboardType =UIKeyboardTypeNumbersAndPunctuation;
        self.txtPrice.delegate=self;
        [self.txtPrice setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.txtPrice setValue:FONT(13) forKeyPath:@"_placeholderLabel.font"];
    }
    return _txtPrice;
}
-(UITextField *)txtTitle
{
    if (!_txtTitle) {
        self.txtTitle = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtTitle.textColor =[UIColor lightGrayColor];
        [self.txtTitle setBorderStyle:UITextBorderStyleNone];
        self.txtTitle.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtTitle.font = FONT(13);
        self.txtTitle.placeholder =@"必填";
        self.txtTitle.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtTitle.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtTitle.returnKeyType = UIReturnKeyDone;
        self.txtTitle.delegate=self;
        [self.txtTitle setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.txtTitle setValue:FONT(13) forKeyPath:@"_placeholderLabel.font"];
    }
    return _txtTitle;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.txtPrice resignFirstResponder];
    [self.txtTitle resignFirstResponder];
    if (indexPath.section==0) {
        switch (indexPath.row) {
            case 2:
            {
                CategarySelectVC *cs =[[CategarySelectVC alloc] init];
                [cs setSelectedCallBack:^(id date , id key) {
                    if (date && [date isKindOfClass:[NSDictionary class]]) {
                        _cateDictionary = date;
                        _isCategoryEnable =YES;
                        _cateId =[NSString stringWithFormat:@"%@",[date objectForKey:@"cate_id"]];
                        _cateString =[NSString stringWithFormat:@"%@",[date objectForKey:@"cate_name"]];
                        [_tbView reloadData];
                    }
                    
                }];
                [self.navigationController pushViewController:cs animated:YES];
            }
                break;
            case 3:
            {
                if (_isCategoryEnable) {
                    BrandSelectVC *cs =[[BrandSelectVC alloc] init];
                    cs.cate_id =_cateId;
                    [cs setSelectedCallBack:^(id date , id key) {
                        if (date && [date isKindOfClass:[NSDictionary class]]) {
                            _brandId =[NSString stringWithFormat:@"%@",[date objectForKey:@"brand_id"]];
                            _brandString =[NSString stringWithFormat:@"%@",[date objectForKey:@"brand_name"]];
                            [_tbView reloadData];
                        }
                        
                    }];
                    [self.navigationController pushViewController:cs animated:YES];
                }else{
                    [[ZToastManager ShardInstance] showtoast:@"请选择分类"];
                }
                
            }
                break;
           
            default:
                break;
        }
    }
}


//发布
-(void)okAction
{
    if(self.txtTitle.text.length==0 )
    {
        [[ZToastManager ShardInstance] showtoast:@"请填写单品名称"];
        return;
    }
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:self.cateId forKey:@"cate_id"];
    [dict setValue:self.cateString forKey:@"cate_name"];
    [dict setValue:[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"group_id"]] forKey:@"group_id"];
    [dict setValue:[NSString stringWithFormat:@"%@",[self.cateDictionary objectForKey:@"group_name"]] forKey:@"group_name"];
    [dict setValue:self.brandId forKey:@"brand_id"];
    [dict setValue:self.brandString forKey:@"brand_name"];
    [dict setValue:@"" forKey:@"photo_id"];
    [dict setValue:self.txtTitle.text forKey:@"title"];
    [dict setValue:self.txtPrice.text forKey:@"price"];
    
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[ShaiDanVC class]]) {
            ShaiDanVC *contro =(ShaiDanVC*)controller;
            contro.cateDictionary = dict;
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}

@end